package com.icss.daos;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.icss.beans.PicVO;

/**
 * 功能：滚动图片业务处理
 * @author 梁海成
 *
 */
public class RollPicDao {
	
	private ResultSet rs=null;
	private Dao dao=new Dao();
	
	/**
	 * 添加图片
	 * @param pvo
	 */
	public void addPic(PicVO pvo){
		String sql = "insert into RollPic(picName,picTitle,picSign) values('"+pvo.getPicName()+"','"+pvo.getPicTitle()+"','n');";
		dao.execute(sql);
		close();
		
	}

	/**
	 * 获取所有图片信息
	 * @return
	 */
	public List<PicVO> getAllPic(){
		String sql = "select * from RollPic";
		List<PicVO> list = new ArrayList<PicVO>();
		rs=dao.query(sql);
		try {
			while(rs.next()){
				PicVO pvo = new PicVO();
				pvo.setPicID(rs.getInt("picID"));
				pvo.setPicName(rs.getString("picName"));
				pvo.setPicTitle(rs.getString("picTitle"));
				pvo.setSign(rs.getString("picSign"));
				list.add(pvo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return list;
	}
	
	/**
	 * 获取一张图片信息
	 * @return
	 */
	public PicVO getOnePic(int id){
		String sql = "select * from RollPic where picID="+id;
		rs = dao.query(sql);
		PicVO pvo = new PicVO();
		
		try {
			if(rs.next()){
				pvo.setPicID(rs.getInt("picID"));
				pvo.setPicName(rs.getString("picName"));
				pvo.setPicTitle(rs.getString("picTitle"));
				pvo.setSign(rs.getString("picSign"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return pvo;
	}
	
	/**
	 * 更新图片信息
	 * @return
	 */
	public boolean updatePic(PicVO pvo,String url){
		String last="select picName from RollPic where picID="+pvo.getPicID();
		Dao dao1 =new Dao();
		rs = dao1.query(last);
		String fileName = "";
		try {
			if(rs.next()){
				fileName = rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File file = new File(url+fileName);
		String sql ="";
		if(file.delete()){
			sql = "update RollPic  set picName='"+pvo.getPicName()+"',picTitle='"+pvo.getPicTitle()+"' where picID="+pvo.getPicID();
			dao.execute(sql);
			dao1.close();
			close();
			return true;
		}else{
			dao1.close();
			return false;
		}
		
	}
	
	public void updatePic2(String title,int id){
	
		String sql = "update RollPic  set picTitle='"+title+"' where picID="+id;
		dao.execute(sql);
		close();
		
	}
	
	/**
	 * 删除图片
	 * @return
	 */
	public void delPic(int id,String url){
		String sql = "delete from RollPic where picID="+id;
		String last="select picName from RollPic where picID="+id;
		Dao dao1 =new Dao();
		rs = dao1.query(last);
		String fileName = "";
		try {
			if(rs.next()){
				fileName = rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File file = new File(url+fileName);
		if(file.delete()){
			dao.execute(sql);
		}
		dao1.close();
		close();
	}
	
	/**
	 * 将图片发布到前台
	 * @return
	 */
	public void publish(int id){
		String sql ="update RollPic set picSign='y' where picID="+id;
		dao.execute(sql);
		close();
	}
	
	/**
	 * 取消分布到前台
	 * @return
	 */
	public void dispublish(int id){
		String sql ="update RollPic set picSign='n' where picID="+id;
		dao.execute(sql);
		close();
	}
	
	/**
	 * 获取要发布到前台的8张图片
	 * @return
	 */
	
	public List<PicVO> getIndexPic(){
		String sql ="select * from RollPic where picSign='y' limit 0,8";
		rs = dao.query(sql);
		List<PicVO> list = new ArrayList<PicVO>();
		try {
			while(rs.next()){
				PicVO vo = new PicVO();
				vo.setPicID(rs.getInt("picID"));
				vo.setPicName(rs.getString("picName"));
				vo.setPicTitle(rs.getString("picTitle"));
				list.add(vo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return list;
	}
	
	
	/**
	 * 关闭连接
	 * @return
	 */
	public void close(){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dao.close();
			
		}
	}
}
